<?php
//$_GET['id']
//
//$_POST['id']
//$_POST['title']
//$_POST['description']
//$_POST['private']

	include 'include/function.php';
	include_once 'include/revision.php';
	
	if(!isset($_user_id))redirect_error(_d('err_need_login'));

	if(isset($_POST['id'])) {
		$editnode_id		=get_to_mysql($_POST['id']);
		$editnode_title		=get_to_mysql($_POST['title']);
		$editnode_des		=get_to_mysql($_POST['description']);
		$editnode_private	=get_to_mysql($_POST['private']);
		if(empty($editnode_private))redirect_error('bad request');
		
		permission_check_edit($_user_id,$editnode_id);
		if(get_node_parent_id($editnode_id)==get_special_page_id('Templates'))$editnode_private='view';
		
		//版本记录
		$row=mysql_fetch_row(oj_query("select description from node where id=$editnode_id "));
		if($row[0]!=stripslashes($_POST['description']))
			insert_revision($editnode_id,'node',$_user_id,'',$row[0],stripslashes($_POST['description']));

		//更新
		$query="update node set title='$editnode_title', description='$editnode_des', has_question='$editnode_private' "
			. ", modify_time=now(), modify_id=$_user_id "
			. "where id=$editnode_id ";
		$ret=oj_query($query);
		
		$now_parent=$editnode_id;
		while($now_parent>=0) {
			$query="select parent from node where id=$now_parent";
			$ret=oj_query($query);
			$row=mysql_fetch_row($ret);
			if(isset($row[0])) {
				$query="update node set child_update_time=now() where id=$now_parent ";
				oj_query($query);
				$now_parent=$row[0];
			} else $now_parent=-1;
		}
		
		redirect("shownode.php?id=$editnode_id");
	}
	
	if(!isset($_GET['id']))redirect_error('Please select the node');
	$editnode_id=get_to_html($_GET['id']);
	permission_check_edit($_user_id,$editnode_id);
	
	$query='select * from node where id=' . $editnode_id;
	$ret=oj_query($query);
	$row=mysql_fetch_assoc($ret);
	//冻结的不能编辑
	if($row['freezed']=='yes')redirect_error(_d('err_node_frozen'));
	
	$page_title = $site_title . ' - ' . _d('title_editnode');
	include 'include/header.php';
	include 'include/form.php';
	
	$editnode_check_title='onChange=\'checkTitle(this,$("node_check_title_box"),"node");\'';
	draw_simple_form('post','editnode.php',array('id'=>$editnode_id),
		array(
			'id'=>array('html',_d('a_nid'),"<a href='shownode.php?id=$editnode_id'>$editnode_id</a>"),
			'title'=>array('input',_d('a_title'),$row['title'],'maxlength=200 ' . $editnode_check_title),
			'check_title'=>array('html','','<div id="node_check_title_box"></div>'),
			'description'=>array('textarea',_d('a_description'),$row['description'],' class="param_use_include param_use_end_include" '),
			'private'=>array('option',_d('a_lock'),$row['has_question'],'list'=>array('no'=>_d('a_no_lock'), 'yes'=>_d('sn_locked'), 'view'=>_d('sn_free_view'))),
			'submit'=>array('submit','',_d('edit_submit'))
		));
	echo '<script type="text/javascript" src="include/textarea.js"></script>';

	include 'include/footer.php';
?>

